﻿using System;
using System.Collections.ObjectModel;
using System.IO;
using System.Management.Automation;

namespace ConsoleTool
{
    public static class PowerShellExtensions
    {
        public static void InvokeScript(this PowerShell ps, string script)
        {
            Console.WriteLine(script);
            ps.AddScript(script).Invoke().PrintPSObject();
        }

        public static void PrintPSObject(this Collection<PSObject> psos)
        {
            foreach (PSObject pso in psos)
            {
                if (pso.BaseObject is String str)
                {
                    Console.WriteLine(str);
                }
                else if (pso.BaseObject is FileInfo fileInfo)
                {
                    Console.WriteLine(fileInfo.Name);
                }
                else
                {
                    Console.WriteLine($"unknow type {pso.BaseObject.GetType().FullName}");
                }
            }
        }
    }
}
